package com.css.fxfzxczh.base.remote;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author Dbj
 * @date 2023年06月09日  14:22
 */
@Service
public class RemoteService {

    @Resource(name = "httpsRestTemplates")
    RestTemplate restTemplate;

    public RestResponse getJCSJ(String url, int tab, JSONObject valueMap){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzxczh");
        headers.set("subsystemCode", "dzdzzhpg");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        RestResponse resp = null;
        try {
            if (tab == 1) {
                JSONObject data = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody().getJSONObject("data");
                resp = RestResponse.succeed(data);
            } else if (tab == 2) {
                JSONObject data = restTemplate.exchange(url, HttpMethod.GET, r, JSONObject.class).getBody().getJSONObject("data");
                resp = RestResponse.succeed(data);
            } else if (tab == 3) {
                JSONArray data = restTemplate.exchange(url, HttpMethod.GET, r, JSONObject.class).getBody().getJSONArray("data");
                resp = RestResponse.succeed(data);
            } else if (tab == 4) {
                JSONArray data = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody().getJSONArray("data");
                resp = RestResponse.succeed(data);
            } else if (tab == 5) {
                JSONArray data = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody().getJSONObject("data").getJSONArray("data");
                resp = RestResponse.succeed(data);
            }else if (tab == 6) {
                JSONArray data = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody().getJSONArray("data");
                resp = RestResponse.succeed(data);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }
}
